package appx.craft.droid.ui;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import appx.craft.droid.R;
import appx.craft.droid.utils.Const;
import appx.craft.droid.utils.Log;

public class ImageViewActivity extends FragmentActivity {
	
	private static final String TAG = ImageViewActivity.class.getCanonicalName();
	ImageView imageview_serverImage;
	String str$ImageName;
	Button btn_close_window;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.debug(TAG,"[ onCreate ] ");
		setContentView(R.layout.activity_image_view);
		
		str$ImageName = getIntent().getExtras().getString(Const.INTENT_IMAGE);
		
		imageview_serverImage = (ImageView)findViewById(R.id.imageview_serverImage);
		btn_close_window = (Button)findViewById(R.id.btn_close_window);
		
		if(str$ImageName != null){
			
			new AsyncTask<Void,Void,Integer>(){
				
				URL myFileUrl =null;     
				HttpURLConnection conn;
				protected void onPreExecute() {
				
				};
				
				@Override
				protected Integer doInBackground(Void... params) {
					try{
						  myFileUrl= new URL(new String(Const.VERIFY_HOST+str$ImageName));
						  conn= (HttpURLConnection)myFileUrl.openConnection();
						  conn.setDoInput(true);
			              conn.connect();
			              final InputStream is = conn.getInputStream();
			              imageview_serverImage.post(new Runnable() {
							
							@Override
							public void run() {
								imageview_serverImage.setImageBitmap(BitmapFactory.decodeStream(is));
							}
						});
			           
					}catch (Exception e) {
						e.printStackTrace();
					}
					return null;
				}
				protected void onPostExecute(Integer result) {
					  	 conn.disconnect();
			             myFileUrl = null;
			              
						
				};
				
			}.execute();
		}
		btn_close_window.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	@Override
	protected void onStart() {
		super.onStart();
		Log.debug(TAG,"[ onStart ] ");
	}
	@Override
	protected void onStop() {
		super.onStop();
		Log.debug(TAG,"[ onStop ] ");
	}
	@Override
	protected void onPause() {
		super.onPause();
		Log.debug(TAG,"[ onPause ] ");
	}
	@Override
	protected void onResume() {
		super.onResume();
		Log.debug(TAG,"[ onResume ] ");
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.debug(TAG,"[ onDestroy ] ");
	}
}
